package com.niiwoo.civet.account.service.local.openAccount2.order;

import com.niiwoo.civet.account.dao.entity.OpenAccountOrder;
import com.niiwoo.civet.account.dao.entity.OpenAccountRecord;
import com.niiwoo.civet.account.dao.mapper.OpenAccountOrderMapperExt;
import com.niiwoo.civet.account.enums.GroupAccountRelevant;
import com.niiwoo.civet.account.enums.OpenAccountOrderStatusEnum;
import com.niiwoo.civet.account.enums.OpenAccountRecordStatusEnum;
import com.niiwoo.civet.account.service.local.openAccount2.dto.*;
import com.niiwoo.civet.account.service.local.openAccount2.lanmao.LanMaoHelper;
import com.niiwoo.civet.account.service.local.openAccount2.type.RegisterTypeable;
import com.niiwoo.civet.base.constant.OrgConstant;
import com.niiwoo.tripod.base.component.PrivacyMasks;
import com.niiwoo.tripod.lanmao.component.LanMaoSequence;
import com.niiwoo.tripod.lanmao.enums.AuthenticateValidTypeEnum;
import com.niiwoo.tripod.lanmao.enums.CredentialsTypeEnum;
import com.niiwoo.tripod.lanmao.enums.UserLimitTypeEnum;
import com.niiwoo.tripod.service.component.SnowflakeIdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class RegisterOrderHandler implements OpenAccountOrderOperation<OpenAccountOrder>, RegisterTypeable {

    @Autowired
    private SnowflakeIdWorker snowflakeIdWorker;

    @Autowired
    private LanMaoSequence lanMaoSequence;

    @Autowired
    private PrivacyMasks privacyMasks;

    @Autowired
    private OpenAccountOrderMapperExt openAccountOrderMapperExt;

    @Override
    public boolean create(OpenAccountOrder order) {
        return openAccountOrderMapperExt.insertSelective(order) > 0;
    }

    @Override
    public boolean update(OpenAccountOrder order) {
        return openAccountOrderMapperExt.updateByPrimaryKeySelective(order) > 0;
    }

    @Override
    public OpenAccountOrder getByRequestNo(String requestNo) {
        return openAccountOrderMapperExt.selectByRequestNo(requestNo);
    }

    @Override
    public boolean isSuccess(OpenAccountOrder order) {
        return OpenAccountOrderStatusEnum.except(order.getStatus(), OpenAccountOrderStatusEnum.SUCCESS);
    }

    @Override
    public OpenAccountOrder getNewOrder(CreateRequest request, OpenAccountRecord record) {
        RegisterCreateRequest registerCreateRequest = (RegisterCreateRequest) request;
        OpenAccountOrder order = new OpenAccountOrder();
        order.setId(snowflakeIdWorker.nextId());
        order.setRequestNo(lanMaoSequence.getRequestNo());
        order.setAccountNo(record.getAccountNo());
        order.setUserId(record.getUserId());
        order.setUserRole(record.getAccountRole());
        order.setRealName(registerCreateRequest.getName());
        order.setCertNo(privacyMasks.encryptPrivacy(registerCreateRequest.getIdCardNo().toUpperCase()));
        order.setCertType(CredentialsTypeEnum.PRC_ID.getValue());
        order.setBindCardNo(privacyMasks.encryptPrivacy(registerCreateRequest.getBankCardNo()));
        order.setMobileNo(privacyMasks.encryptPrivacy(registerCreateRequest.getBankCardMobileNo()));
        order.setCheckType(AuthenticateValidTypeEnum.LIMIT.getValue());
        order.setUserLimitType(UserLimitTypeEnum.ID_CARD_NO_UNIQUE.getValue());
        order.setAuthList(LanMaoHelper.getAuthTextByRole(registerCreateRequest.getRole()));
        order.setStatus(OpenAccountOrderStatusEnum.PROCESSING.getValue());
        // 你我金融自有平台
        if(OrgConstant.NIIWOO_ORG_ID.equals(request.getOrgId())){
            order.setIsRelevantGroupAccount(GroupAccountRelevant.YES.getValue());// 默认开通集团账户
        }else{
            order.setIsRelevantGroupAccount(GroupAccountRelevant.NO.getValue());// 机构默认不开通集团账户
        }
        order.setOrgId(request.getOrgId());
        order.setCreateTime(new Date());
        return order;
    }

    @Override
    public OrderBasicInfo getOrderBasicInfo(OpenAccountOrder order) {
        OrderBasicInfo orderBasicInfo = new OrderBasicInfo();
        orderBasicInfo.setAccountNo(order.getAccountNo());
        orderBasicInfo.setRole(order.getUserRole());
        orderBasicInfo.setUserId(order.getUserId());
        orderBasicInfo.setAuthList(order.getAuthList());
        orderBasicInfo.setIsInitAccount(true);
        return orderBasicInfo;
    }

    @Override
    public OpenAccountOrder getUpdateOrder(OpenAccountOrder oldOrder, UpdateRequest request, OpenAccountRecordStatusEnum recordStatus) {
        RegisterUpdateRequest registerUpdateRequest = (RegisterUpdateRequest) request;
        OpenAccountOrder updateOrder = new OpenAccountOrder();
        updateOrder.setId(oldOrder.getId());
        if (registerUpdateRequest.getAccessType() != null) {
            updateOrder.setCheckType(registerUpdateRequest.getAccessType().getValue());
        }
        if (registerUpdateRequest.getBankCode() != null) {
            updateOrder.setBankCode(registerUpdateRequest.getBankCode().getCode());
        }
        // 个人账户使用身份证开户，没有审核状态
        switch (recordStatus) {
            case SUCCESS: {
                updateOrder.setStatus(OpenAccountOrderStatusEnum.SUCCESS.getValue());
            }
            break;
            case EXPIRED:
            case FAILURE: {
                updateOrder.setStatus(OpenAccountOrderStatusEnum.FAILURE.getValue());
            }
            break;
            case AUDITING:
            case PROCESSING:
            case AUDIT_REJECT:
            case AUDIT_RETURN:
            default:
                updateOrder.setStatus(oldOrder.getStatus());
        }
        updateOrder.setErrorCode(registerUpdateRequest.getErrorCode());
        updateOrder.setErrorMessage(registerUpdateRequest.getErrorMessage());
        return updateOrder;
    }


}
